using System;
using System.Collections.Generic;
using Mall.Api.Entity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using Mall.Api.ParamModel;
using Newtonsoft.Json;
using Mall.Api.Repository;
using Mall.Api.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;
using Mall.Api.Filters;

namespace Mall.Api.Controllers
{
    /// <summary>
    /// 订单管理
    /// </summary>


    // [Authorize]

    [ApiController]
    [Route("order")]
    public class OrderController : ControllerBase
    {
        private IRepository<Order> _order;
        private IRepository<UserOrder> _userOrder;
        private IRepository<CommoditySKU> _commoditySKU;
        private IRepository<Commodity> _commodity;
        private IRepository<CommodityPropertyValue> _commodityPropertyValue;


        private readonly IConfiguration _configuration;

        public OrderController(IConfiguration configuration, IRepository<Order> order, IRepository<UserOrder> userOrder, IRepository<CommoditySKU> commoditySKU, IRepository<Commodity> commodity, IRepository<CommodityPropertyValue> commodityPropertyValue)
        {
            _configuration = configuration;
            _order = order;
            _userOrder = userOrder;
            _commoditySKU = commoditySKU;
            _commodity = commodity;
            _commodityPropertyValue = commodityPropertyValue;
        }

        /// <summary>
        /// 获取订单列表
        /// </summary>
        /// <param name="pager"></param>
        /// <returns></returns>
        [HttpGet]
        public dynamic Get([FromQuery] Pager pager)
        {//get请求默认从uri从获取参数，所以此处应该加上FromQuery的特性才能正确获得参数
            var pageIndex = pager.PageIndex;
            var pageSize = pager.PageSize;

            // 获得所有满足条件的记录
            var userOrders = from userorder in _userOrder.Table
                             join order in _order.Table on userorder.Id equals order.UserOrderId
                             join commoditySKU in _commoditySKU.Table on order.CommoditySKUId equals commoditySKU.Id
                             join commodity in _commodity.Table on commoditySKU.CommodityId equals commodity.Id
                             join commodityPropertyValue in _commodityPropertyValue.Table on
                             commoditySKU.CommodityPropertyValueId equals commodityPropertyValue.Id
                             where userorder.IsDeleted == false
                             select new
                             {

                                 Id = userorder.Id,
                                 ProductName = commodity.ProductName,
                                 PropertyValue = commodityPropertyValue.PropertyValue,
                                 OrderCode = userorder.OrderCode,
                                 Total = userorder.Total,
                                 Number = userorder.Number,
                                 IsActived = userorder.IsActived,
                                 CreatedTime = userorder.CreatedTime


                             };
            if (!string.IsNullOrEmpty(pager.KeyWord))
            {
                userOrders = userOrders.Where(u => u.OrderCode.Contains(pager.KeyWord) || u.ProductName.Contains(pager.KeyWord));
            }
            // 对获得的记录分页
            var u = userOrders.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            // 返回的数据里，带有数据和分页信息
            var res = new
            {
                Code = 1000,
                Data = new { Data = u, Pager = new { pageIndex, pageSize, rowsTotal = userOrders.Count() } },
                Msg = "获取仓库列表成功^_^"
            };
           
            return res;
        }









        /// <summary>
        /// 真删除指定订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            var classes = _userOrder.GetById(id);
            if (classes != null)
            {
                _userOrder.Delete(id);
                return new
                {
                    Code = 200,
                    Msg = string.Format("删除指定id为{0}的订单成功", id)
                };
            }
            else
            {
                return new
                {
                    Code = 1004,
                    Data = "",
                    Msg = "指定Id的订单不存在，请确认后重试"
                };
            }

        }





    }

}




